Completed
Push — master ( f48291...a65f2c )
by greg
74:59
created

values.js ➔ ... ➔ ???   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 3
dl 0
loc 14
rs 9.2
nop 1
1
import {
2
  cmsData
3
} from '../../'
4
5
function recurseDeleteKey(currentLevel, arrayKeyAttr) {
6
  var currentArray = arrayKeyAttr.slice(0)
7
8
  if (arrayKeyAttr.length === 1) {
9
    delete currentLevel[arrayKeyAttr[0]]
10
  }
11
12
  Array.prototype.forEach.call(currentArray, (key) => {
13
    if(currentLevel[key] != null) {
14
      currentLevel = currentLevel[key]
15
      currentArray.shift()
16
      recurseDeleteKey(currentLevel, currentArray)
17
      if(typeof currentLevel === 'object' && Object.prototype.toString.call(currentLevel) === '[object Array]') {
18
        Array.prototype.forEach.call(currentLevel, (item) => {
19
          recurseDeleteKey(item, currentArray)
20
        })
21
      }else {
22
        recurseDeleteKey(currentLevel, currentArray)
23
      }
24
    }
25
  })
26
}
27
28
export function removeDuplicate(text, json) {
29
  var regAbe = /{{abe[\S\s].*?duplicate=['|"]([\S\s].*?['|"| ]}})/g
30
  var matches = text.match(regAbe)
31
  if(matches != null){
32
33
    Array.prototype.forEach.call(matches, (match) => {
34
      var keyAttr = cmsData.regex.getAttr(match, 'key')
35
36
      if(match != null) {
37
        var arrayKeyAttr = keyAttr.split('.')
38
        recurseDeleteKey(json, arrayKeyAttr)
39
      }
40
    })
41
  }
42
43
  return json
44
}